/**
 * fileName: TabTag.java
 *     Date: 2012-6-5
 **/
package com.stranger.common.tag.tabs;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.Tag;


/**
 * @作者 stranger.lin
 *
 * @时间 2012-6-5
 */
public class TabTag extends BodyTagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2367103994758808738L;
	
	private String id;
	private String name;
	private 	JspWriter out;

	@Override
	public void setBodyContent(BodyContent b) {
		this.bodyContent=b;
	}
	@SuppressWarnings("deprecation")
	@Override
	public int doStartTag() throws JspException {
		try {
			out=pageContext.getOut();
			Tag tag=findAncestorWithClass(this, TabsTag.class);
			if (tag==null) {
				throw new JspException("tab标签必须在tabs标签内");
			}
			if (id==null&&"".equals(id.trim())) {
				throw new JspException("tab标签id不能为空");
			}
			
			
			
			TabsTag tabs=(TabsTag)tag;
			Tab tab=new Tab();
			tab.setId(id);
			tab.setName(name);
			
			tabs.getTabs().add(tab);
			StringBuffer html=new StringBuffer("<div id=\""+id+"\">");
			out.print(html);
			out.flush();
			//out.close();
			id=null;
		} catch (Exception e) {
			throw new JspException(e.getMessage());
		}
		
		return EVAL_BODY_TAG;
	}
	
	

	@Override
	public int doEndTag() throws JspException {
		try {
			out=pageContext.getOut();
			StringBuffer html=new StringBuffer();
			if (bodyContent!=null) {
				html.append(bodyContent.getString());
			}
			
			html.append("</div>");
			out.print(html);
			out.flush();
			//out.close();
			
		} catch (Exception e) {
			throw new JspException(e.getMessage());
		}
		return EVAL_PAGE;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}



	public String getName() {
		return name;
	}



	public void setName(String name) {
		this.name = name;
	}
	
	
	
}
